#include <vector>
#include <iostream>
using std::cout;
using std::vector;

// 采用左闭右开的方式
class Solution
{
public:
    vector<vector<int>> generateMatrix(int n)
    {
        vector<vector<int>> v1(n, vector<int>(n, 0));

        int startX = 0;
        int startY = 0;
        int offset = 1;
        int circleNum = n / 2;
        int num = 1;
        while (1)
        {
            if (circleNum == 0)
                break;
            // top edge
            for (startY; startY < n - offset; ++startY)
                v1[startX][startY] = num++;

            // right edge
            for (startX; startX < n - offset; ++startX)
                v1[startX][startY] = num++;

            // bottom edge
            for (startY; startY >= offset; --startY)
                v1[startX][startY] = num++;

            // left edge
            for (startX; startX >= offset; --startX)
                v1[startX][startY] = num++;

            // cout << "startX:" << startX << "\n";
            // cout << "startY:" << startY << "\n";
            // break;
            circleNum--;
            offset++;
            startX++;
            startY++;
        }

        if (n % 2 != 0)
            v1[n / 2][n / 2] = n * n;
        return v1;
    }
};

int main(int argc, char const *argv[])
{
    Solution s;
    for (auto &it : s.generateMatrix(5))
    {
        for (auto &i : it)
        {
            cout << i << " ";
        }
        cout << "\n";
    }
    getchar();
    return 0;
}
